package com.bukaedu.info.manager.dao;

import com.bukaedu.info.manager.domain.Student;

public class StudentDao {
    private static Student[] stus = new Student[3];// null
    // 查询所有
    public  Student[] findAllStudent() {
        return stus;
    }

    public boolean addStudent(Student stu) {
        // 添加学生对象到数组
        // -1 代表数组装满了
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if(student == null){
                index = i;
                break;
            }
        }
        if(index == -1){
            // 没有空位 ， 装满了
            return  false;
        }else{
            // 没装满，正常添加
            stus[index] = stu;
            System.out.println("数据："+stus[index]);
            return true;
        }
    }

    public void deleteStudentById(String delId) {
        int index = getIndex(delId);
            stus[index] = null;
    }

    public  int getIndex(String id){
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if(stu != null && stu.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStu) {
        int index = getIndex(updateId);
        stus[index] = newStu;
    }
}
